#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#*===========================================================================*/
#*                                                                           */
#* Module Name:  cfgct                                                       */
#*                                                                           */
#* Description:                                                              */
#*      Script to configure the RSCT subsystems at installation              */
#*                                                                           */
#* N.B. This command is intended to be executed by the RMC subsystem only !! */
#*                                                                           */
#*===========================================================================*/
#*   @(#)10   1.11   src/rsct/rmc/mcdaemon/cfgct.sh, mcdaemon, rsct_rzauh, rzauh0431a 11/7/02 19:53:00

# Set path to known value
PATH=/usr/sbin/rsct/install/bin:/usr/sbin/rsct/bin:/usr/bin:/bin:/usr/sbin
export PATH

CTROOTDIR=/var/ct		# root directory for all RSCT run-time data
rc=0									# return code

#============================================================================
#
# Test for debug environment variable. If its value is 1, exit immediately
# with a 0 exit value. If it's value is 2, set INUBOSTYPE for testing BOS
# installation. If it's value is 3, set INUCLIENTS for NIM testing
#
#============================================================================

if [[ ${CFGCT_DEBUG:-0} -eq 1 ]]
then
	exit 0
fi

if [[ ${CFGCT_DEBUG:-0} -eq 2 ]]
then
	export INUBOSTYPE=1
fi

if [[ ${CFGCT_DEBUG:-0} -eq 3 ]]
then
	export INUCLIENTS=1
fi

umask 022


#============================================================================
#
# Step 0: parse cmdline options
#
#============================================================================
CfgCtInstall=0
Argcount=0
while getopts ":I" opt
do
    case $opt in
        I ) CfgCtInstall=1; Argcount=$((Argcount + 1));;
        ? ) exit 0;;
    esac
done

#============================================================================
#
# Step 1: make the node id
#
#============================================================================

if ! mknodeid -k
then
	exit 1
fi

#============================================================================
#
# Step 2: check if /var/ct/IW exists. If so, then configuration has already
# been done. Set-up the ctposti script to run on AIX if called during install,
# otherwise exit.
#
#============================================================================

if [[ -a $CTROOTDIR/IW ]]
then
	if [ $CfgCtInstall -eq 1 ]
	then
		if [[ $(uname -s) = "AIX" ]]
		then
			/usr/sbin/rsct/install/bin/ctinstall -t I -f rsct.core.rmc -g RSCT_ALL
			rc=$?
		fi
	fi
	exit 0
fi

#============================================================================
#
# Step 3: create required directories and configuration files in /var
#
#============================================================================

# get machine ID

if [[ $(uname -s) = "AIX" ]]
then
	LC_ALL=C uname -x | read r1 r2 MID junk
else
	MID=$(uuidgen)
fi

# create directories, using the machine ID as the cluster ID

for name in                        \
	$CTROOTDIR/$MID                \
	$CTROOTDIR/$MID/cfg            \
	$CTROOTDIR/$MID/lck            \
	$CTROOTDIR/$MID/log            \
	$CTROOTDIR/$MID/registry       \
	$CTROOTDIR/$MID/run            \
	$CTROOTDIR/$MID/soc
do
	if [[ ! -a $name ]]
	then
		if ! mkdir $name
		then
			exit 1
		fi
		chown bin $name
		chgrp bin $name
		chmod 775 $name
	fi
done

# link the cluster name

ln -s $CTROOTDIR/$MID $CTROOTDIR/IW

# make config files

print "$MID IW" > $CTROOTDIR/cfg/clusters
print "$MID" > $CTROOTDIR/cfg/default_cluster
print "$MID" > $CTROOTDIR/cfg/current_cluster

print "1 $MID localhost $MID IW 1" > $CTROOTDIR/IW/cfg/nodedef.cfg

# create the "first start" file

touch $CTROOTDIR/first_start

#============================================================================
#
# Step 4: add the RMC subsystem and start it, unless INUCLIENTS is set
# or INUBOSTYPE is set
#
#============================================================================

# add security subsystem to SRC first

ctcasctrl -a 

if rmcctrl -a
then
	if [[ -z "$INUCLIENTS" && -z "$INUBOSTYPE" ]]
	then
		if ! rmcctrl -s
		then
			exit 1
		fi
	fi
else
	exit 1
fi

exit 0
